package com.pf.business.export.handler;

import com.alibaba.excel.metadata.data.WriteCellData;
import com.alibaba.excel.util.BooleanUtils;
import com.alibaba.excel.write.handler.CellWriteHandler;
import com.alibaba.excel.write.handler.context.CellWriteHandlerContext;
import com.alibaba.excel.write.metadata.style.WriteCellStyle;
import com.alibaba.excel.write.metadata.style.WriteFont;
import com.google.common.collect.Lists;
import com.pf.business.export.helper.ExportDataHelper;
import com.pf.utils.ExportExcelUtils;
import org.apache.poi.common.usermodel.HyperlinkType;
import org.apache.poi.ss.usermodel.*;

import java.util.List;
import java.util.Map;

/**
 * @author PanFei
 * @version 1.0.0
 * @createTime 2023/7/16 13:07
 */
public class CriteriaCellWriteHandler implements CellWriteHandler {

    /**
     * 业务sheet页名称
     */
    private String sheetName;

    /**
     * 数据起始行下标
     */
    private Integer dataStartRowIndex;

    public CriteriaCellWriteHandler(String sheetName, Integer dataStartRowIndex) {
        this.sheetName = sheetName;
        this.dataStartRowIndex = dataStartRowIndex;
    }

    @Override
    public void afterCellDispose(CellWriteHandlerContext context) {
        // 判断不是表头，如果是fill的情况，这里会是null。所以用not true
        if (BooleanUtils.isNotTrue(context.getHead())) {
            Workbook workbook = context.getWriteSheetHolder().getSheet().getWorkbook();
            Sheet sheet = context.getWriteSheetHolder().getSheet();
            WriteCellData<?> firstCellData = context.getFirstCellData();
            Integer columnIndex = firstCellData.getColumnIndex();
            Cell cell = context.getCell();
            // 设置名称管理器
            if (columnIndex == 0) {
                // 为【准则编码】列设置超链接
                CreationHelper creationHelper = workbook.getCreationHelper();
                Hyperlink hyperlink = creationHelper.createHyperlink(HyperlinkType.URL);
                hyperlink.setAddress("https://www.baidu.com?wd=" + cell.getStringCellValue());
                cell.setHyperlink(hyperlink);
                // 设置样式
                WriteCellStyle writeCellStyle = firstCellData.getOrCreateStyle();
                // 设置字体蓝色下划线
                WriteFont writeFont = new WriteFont();
                writeFont.setUnderline(Font.U_SINGLE);
                writeFont.setColor(IndexedColors.BLUE1.getIndex());
                // 灰色背景
                writeCellStyle.setFillPatternType(FillPatternType.SOLID_FOREGROUND);
                writeCellStyle.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
                writeCellStyle.setWriteFont(writeFont);
            }

            // 下拉列表列设置时，只需要设置一次
            if (firstCellData.getRowIndex().equals(dataStartRowIndex)) {
                if (columnIndex == 1) {
                    // 【准则类型】列
                    ExportExcelUtils.createBoundedDropDownList(sheet, columnIndex, Lists.newArrayList("智选", "车规", "其他"));
                } else if (columnIndex == 2) {
                    // 【准则名称】列
                    List<String> optionList = ExportDataHelper.getCriteriaNameList();
                    ExportExcelUtils.createUnlimitedDropDownList(workbook, sheetName, columnIndex, 0, optionList);
                    // 先生成excel名称管理器
                    Map<String, List<String>> targetMap = ExportDataHelper.getCriteriaMap();
                    ExportExcelUtils.createSheetNameManger(workbook, 1, targetMap, true);
                } else if (columnIndex == 3) {
                    // 【一级分类】列。 $C2是【准则名称】列
                    ExportExcelUtils.createCascadeDropDownList(workbook, sheetName, 3, "\"_\"&$C2", true);
                } else if (columnIndex == 4) {
                    // 【二级分类】列。$C2是【准则名称】列；$D2是【一级分类】列
                    ExportExcelUtils.createCascadeDropDownList(workbook, sheetName, 4, "\"_\"&$C2&\"_\"&$D2", true);
                }
            }
        }
        // 这样样式就设置好了 后面有个FillStyleCellWriteHandler 默认会将 WriteCellStyle 设置到 cell里面去 所以可以不用管了
    }
}
